package com.atnjupt.admin.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author Wizarder
 * @create 2021-10-13 17:47
 */
@Slf4j
@Controller
public class FormController {
    @GetMapping("/form_layouts")
    public String toFormPage(){
        return "form/form_layouts";
    }
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("password") String password,
                         @RequestPart("headimg")MultipartFile multipartFile,
                         @RequestPart("lifeimg")MultipartFile[] multipartFiles) throws IOException {
       int length = multipartFiles.length;
        log.info("email={},password={},headImg={},lifeImg{}",email,password,multipartFile.getSize(),
                multipartFiles.length);
        uoloadImg(multipartFile,multipartFiles);//用异步方法来上传完美解决

        return "index";
    }
    @Async
    public void  uoloadImg(MultipartFile head,MultipartFile[] photos) throws IOException {
        if (photos.length > 0){
            for (MultipartFile file : photos) {
                if (!file.isEmpty()){
                    String originalFilename = file.getOriginalFilename();
                    file.transferTo(new File("G:\\上传图片\\"+originalFilename));
                }

            }
        }
        if (!head.isEmpty()){
            String originalFilename = head.getOriginalFilename();
            head.transferTo(new File("G:\\上传图片\\" + originalFilename));
        }
    }
}
